<template>
  <el-card v-loading="cardLoading" class="lasted-login-user">
    <p :style="{'font-size': fontSize}" class="title">平台公告</p>
    <div class="imprint-content">
      <el-scrollbar style="height: 100%;">
        <template v-if="announceList && announceList.length">
          <div v-for="(item) in announceList" :key="item.id" :title="item.title" :class="{'mb8': showMobilePage}" class="imprint-item" @click="toHelpInfo(item)">
            <div class="dot" />
            <span :style="{fontSize: showMobilePage ? '12px' : '14px'}" class="ellipsisTwo"> {{ item.title }} </span>
          </div>
        </template>
        <EmptyPlaceholder v-else :empty-img="emptyImg" style="margin-top:20px;" text="暂无平台公告" image-size="90px"/>
      </el-scrollbar>
    </div>
    <div :class="{'mobile-user-content': showMobilePage}" class="lasted-login-user-content">
      <span>最近登录：</span>
      <div class="avatar-list">
        <el-tooltip v-for="(item, i) in lastedLoginUser" :key="i" :content="item.userName+`${item.jobNumber?`(${item.jobNumber})`:''}`" placement="top">
          <div :title="item.userName" class="avatar">
            <img :src="item.avatar || userAvatar" alt="avatar">
          </div>
        </el-tooltip>
      </div>
      <i class="el-icon-arrow-right" @click="showTodayVisitors" />
    </div>
  </el-card>
</template>
<script>
import userAvatar from '@/assets/user-avatar.png';
import { helpList } from '@/api/help';
import { getDataInfo } from '@/api/home';
import EmptyPlaceholder from '@/components/EmptyPlaceholder';
import emptyImg from '@/assets/empty-data-icon.png';
import { EventBus } from '@/utils/eventBus';
import { mapState } from 'vuex';

export default {
  name: 'PlatformCard',
  components: { EmptyPlaceholder },
  props: {
    styleConfig: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      userAvatar,
      emptyImg,
      cardLoading: false,
      announceList: [],
      lastedLoginUser: []
    };
  },
  computed: {
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    }),
    fontSize() {
      if (!this.styleConfig || !this.styleConfig.fontSize) {
        return '14px';
      } else {
        return `${this.styleConfig.fontSize}px`;
      }
    }
  },
  created() {
    this.getHomeInfo();
    this.getAnnounceList();
  },
  methods: {
    toHelpInfo(article) {
      this.$router.push({
        name: 'helpDetail',
        params: {
          id: article.id,
          model: article.model || 'announce'
        }
      });
    },
    getHomeInfo() {
      this.cardLoading = true;
      getDataInfo().then(res => {
        this.cardLoading = false;
        this.lastedLoginUser = res.data && res.data.lastedLoginUser;
      }).catch(_ => {
        this.cardLoading = false;
      });
    },
    getAnnounceList() {
      helpList({ model: 'announce', sort: 'createTime' }).then(res => {
        this.announceList = res.data.items;
      });
    },
    showTodayVisitors() {
      EventBus.$emit('showTodayVisitors');
    }
  }
};
</script>
<style lang="scss">
.lasted-login-user {
  border-radius: 4px;
  box-shadow: 2px 2px 8px 0 #DDE1EA;
  border: none;
  height: 100%;
  .el-card__body {
    padding: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin: 0;
  }
  .imprint-content {
    padding-top: 8px;
    font-size: 14px;
    font-weight: 300;
    color: #333;
    flex: 1;
    overflow-y: auto;
    .imprint-item {
      cursor: pointer;
      display: flex;
      padding-right: 12px;
      margin-bottom: 16px;
      line-height: 1.2;
      align-items: center;
      color: #999;
      .dot {
        display: inline-block;
        min-width: 5px;
        height: 5px;
        background-color: #dddddd;
        border-radius: 50%;
        margin-right: 8px;
      }
    }
    .mb8 {
      margin-bottom: 8px;
      color: #666;
    }
  }
  .lasted-login-user-content {
    padding-top: 16px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    overflow: hidden;
    min-height: 32px;
    border-top: 1px solid #EBEEFD;
    span {
      white-space: nowrap;
      font-size: 14px;
      font-weight: 300;
    }
    .avatar-list {
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      height: 33px;
      overflow: hidden;
      margin-right: -12px;
    }
    .el-icon-arrow-right {
      cursor: pointer;
    }
  }
  .mobile-user-content {
    span {
      font-size: 10px;
      color: #666;
    }
  }
  .avatar {
    margin-bottom: 5px;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    flex-shrink: 0;
    margin-right: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 32px;
      height: 32px;
      border-radius: 50%;
    }
    &:last-child {
      margin-right: 0;
    }
  }
}
</style>
